<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>react-demo-23</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="../../../js/font.css" rel="stylesheet" />
  </head>
  <style lang="scss">
    * {
      font-family: "Noto Sans SC", sans-serif;
    }

    body {
      display: flex;
      justify-content: flex-end;
    }

    .title {
      display: inline-flex;
      border: 1px solid blue;
      border-radius: 3px;
    }
  </style>
  <body>
    <!-- 准备容器 -->
    <div id="main"></div>

    <!-- 引入react核心库 -->
    <script
      type="text/javascript"
      src="../../../js/react.development.min.js"
    ></script>
    <!-- 引入react-dom，用于操作dom -->
    <script
      type="text/javascript"
      src="../../../js/react-dom.development.min.js"
    ></script>

    <!-- 引入prop-types -->
    <script src="../../../js/prop-types.js"></script>

    <!-- 引入babel，用于编译jsx -->
    <script src="../../../js/babel.min.js"></script>

    <!-- 此处一定要写babel，用于提示babel对该内容进行编译 -->
    <script type="text/babel">
      class Demo extends React.Component {
        state = {
          count: 0,
        };
        inc = () => {
          const { count } = this.state;
          this.setState({
            count: count + 1,
          });
        };
        unmountComponent = () => {
          ReactDOM.unmountComponentAtNode(document.getElementById("main"));
        };
        forceUpdateHook = () => {
          this.forceUpdate();
        };
        constructor(props) {
          super(props);
          console.log("constructor hook");
        }
        // 可以使用强制更新跳过
        shouldComponentUpdate() {
          console.log("shouldComponentUpdate hook");
          return true;
        }
        // 组件将要挂载的钩子
        componentWillMount() {
          console.log("componentWillMount hook");
        }
        componentWillUpdate() {
          console.log("componentWillUpdate hook");
        }
        componentDidUpdate() {
          console.log("componentDidUpdate hook");
        }
        render() {
          console.log("render hook");
          return (
            <div>
              <h2>Amount: {this.state.count}</h2>
              <button onClick={this.inc}>Inc</button>
              <button onClick={this.unmountComponent}>unmount</button>
              <button onClick={this.forceUpdateHook}>forceUpdate</button>
            </div>
          );
        }
        componentDidMount() {
          console.log("componentDidMount hook");
        }
        componentWillUnmount() {
          console.log("componentWillUnmount hook");
        }
      }

      class ParentDemo extends React.Component {
        state = {
          family: "Ari",
        };
        changeFamily = () => {
          this.setState({
            family: "Haywood",
          });
        };
        // 第一次不会调用，且新版本不会调用，先于shouldUpdate
        componentWillReceiveProps() {
          console.log("componentWillReceiveProps hook");
        }

        render() {
          return (
            <div>
              Parent - {this.state.family}
              <button onClick={this.changeFamily}>Change</button>
              <ChildDemo family={this.state.family} />
            </div>
          );
        }
      }

      class ChildDemo extends React.Component {
        render() {
          return <div>Child from {this.props.family}</div>;
        }
      }

      ReactDOM.render(<ParentDemo />, document.getElementById("main"));
    </script>
  </body>
</html>
